﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FineSoft.Controls
{
	public class Page : System.Windows.Controls.Grid
	{
		//public string Title
		//{
		//    get
		//    {
		//        return this.TitleBox.Text;
		//    }
		//    set
		//    {
		//        this.TitleBox.Text = value;
		//    }
		//}

		public FineSoft.Models.Document Document
		{
			get
			{
				return (FineSoft.Models.Document)this.EditorContainer.DataContext;
			}
			set
			{
				if (value == this.EditorContainer.DataContext)
					return;
				value.OpenForEditor();
				this.EditorContainer.DataContext = value;
				this.WebBrowser.Navigate(new System.Uri(value.File.EditorFilePath));
			}
		}

		public FineSoft.Models.ComponentObject SelectedComponent
		{
			get
			{
				if (this.ContainerConfigContainer.DataContext == null || !(this.ContainerConfigContainer.DataContext is FineSoft.Models.ComponentObject))
					this.ContainerConfigContainer.DataContext = this.Document.ViewPortObject;
				return (FineSoft.Models.ComponentObject)this.ContainerConfigContainer.DataContext;
			}
			set
			{
				//this.RaisePropertyChanging("SelectedComponent");
				this.ContainerConfigContainer.DataContext = value;
				//this.RaisePropertyChanged("SelectedComponent");
			}
		}

		private System.Windows.Controls.DockPanel _EditorContainer;
		protected System.Windows.Controls.DockPanel EditorContainer
		{
			get
			{
				if (this._EditorContainer == null)
				{
					this._EditorContainer = new System.Windows.Controls.DockPanel();

					this._EditorContainer.Children.Add(this.TitleContainer);
					System.Windows.Controls.DockPanel.SetDock(this.TitleContainer, System.Windows.Controls.Dock.Top);

					this._EditorContainer.Children.Add(this.ContainerConfigContainer);
					System.Windows.Controls.DockPanel.SetDock(this.ContainerConfigContainer, System.Windows.Controls.Dock.Top);

					this._EditorContainer.Children.Add(this.WebBrowser);
				}
				return this._EditorContainer;
			}
		}

		private System.Windows.Controls.Grid _TitleContainer;
		protected System.Windows.Controls.Grid TitleContainer
		{
			get
			{
				if (this._TitleContainer == null)
				{
					this._TitleContainer = new System.Windows.Controls.Grid();
					this._TitleContainer.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition { Width = System.Windows.GridLength.Auto });
					this._TitleContainer.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());

					System.Windows.Controls.TextBlock textBlock = new System.Windows.Controls.TextBlock();
					textBlock.Text = "Page Title：";
					textBlock.Background = System.Windows.Media.Brushes.Transparent;
					textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
					this._TitleContainer.Children.Add(textBlock);
					System.Windows.Controls.Grid.SetColumn(textBlock, 0);

					this._TitleContainer.Children.Add(this.TitleBox);
					System.Windows.Controls.Grid.SetColumn(this.TitleBox, 1);
				}
				return this._TitleContainer;
			}
		}

		private System.Windows.Controls.Grid _ContainerConfigContainer;
		protected System.Windows.Controls.Grid ContainerConfigContainer
		{
			get
			{
				if (this._ContainerConfigContainer == null)
				{
					this._ContainerConfigContainer = new System.Windows.Controls.Grid();
					this._ContainerConfigContainer.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition { Width = System.Windows.GridLength.Auto });
					this._ContainerConfigContainer.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());

					System.Windows.Controls.TextBlock textBlock = new System.Windows.Controls.TextBlock();
					textBlock.Text = "Add widgets to：";
					textBlock.Background = System.Windows.Media.Brushes.Transparent;
					textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
					this._ContainerConfigContainer.Children.Add(textBlock);
					System.Windows.Controls.Grid.SetColumn(textBlock, 0);

					this._ContainerConfigContainer.Children.Add(this.ContainerConfigList);
					System.Windows.Controls.Grid.SetColumn(this.ContainerConfigList, 1);
				}
				return this._ContainerConfigContainer;
			}
		}

		private System.Windows.Controls.ComboBox _ContainerConfigList;
		protected System.Windows.Controls.ComboBox ContainerConfigList
		{
			get
			{
				if (this._ContainerConfigList == null)
				{
					this._ContainerConfigList = new System.Windows.Controls.ComboBox();
					this._ContainerConfigList.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

					this._ContainerConfigList.ItemTemplate = new System.Windows.DataTemplate();
					System.Windows.FrameworkElementFactory factory = new System.Windows.FrameworkElementFactory(typeof(System.Windows.Controls.TextBlock));
					factory.SetBinding(System.Windows.Controls.TextBlock.TextProperty, new System.Windows.Data.Binding("Key.Name"));
					this._ContainerConfigList.ItemTemplate.VisualTree = factory;

					this._ContainerConfigList.SetBinding(System.Windows.Controls.ComboBox.IsEnabledProperty, "IsContainer");
					this._ContainerConfigList.SetBinding(System.Windows.Controls.ComboBox.ItemsSourceProperty, "Containers");
					this._ContainerConfigList.SetBinding(System.Windows.Controls.ComboBox.SelectedItemProperty, "SelectedContainer");
				}
				return this._ContainerConfigList;
			}
		}

		private System.Windows.Controls.TextBox _TitleBox;
		protected System.Windows.Controls.TextBox TitleBox
		{
			get
			{
				if (this._TitleBox == null)
				{
					this._TitleBox = new System.Windows.Controls.TextBox();
					this._TitleBox.SetBinding(System.Windows.Controls.TextBox.TextProperty, "Title");
				}
				return this._TitleBox;
			}
		}

		private System.Windows.Controls.WebBrowser _WebBrowser;
		protected System.Windows.Controls.WebBrowser WebBrowser
		{
			get
			{
				if (this._WebBrowser == null)
				{
					this._WebBrowser = new System.Windows.Controls.WebBrowser();
				}
				return this._WebBrowser;
			}
		}

		private Cover _Cover;
		public Cover Cover
		{
			get
			{
				if (this._Cover == null)
				{
					this._Cover = new Cover();
					this._Cover.Click += new EventHandler<System.Windows.Input.MouseButtonEventArgs>(Cover_Click);
					this._Cover.PlacementTarget = this.WebBrowser;
					this._Cover.SetBinding(System.Windows.Controls.Primitives.Popup.DataContextProperty, new System.Windows.Data.Binding { Source = this, Path = new System.Windows.PropertyPath("DataContext") });
				}
				return this._Cover;
			}
		}

		public Page()
		{
			this.Children.Add(this.EditorContainer);
		}

		protected override void OnInitialized(EventArgs e)
		{
			base.OnInitialized(e);
		}

		protected override void OnRender(System.Windows.Media.DrawingContext dc)
		{
			base.OnRender(dc);
			if (this._Cover == null)
				this.Children.Add(this.Cover);
		}

		void Cover_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			System.Windows.Point position = e.GetPosition(this.Cover);
			position.Y -= 43;
			this.SelectComponent(position);
		}

		protected void AddComponent(FineSoft.Models.ComponentClass componentType)
		{
			FineSoft.Models.ComponentObject component = this.Document.AddComponent(this.SelectedComponent.NearestContainer, componentType);
			this.WebBrowser.RefreshComponentInItems(component.RefreshContainer.ParentComponent == null ? null : component.RefreshContainer.ParentComponent.Id, component.RefreshContainer.Id, component.RefreshContainer.ConfigsString);
			this.SelectComponent(component);
		}

		protected void SelectComponent(System.Windows.Point position)
		{
			string[] componentIds = this.WebBrowser.GetComponentIdsAtPosition(this.Document.AllComponentIds, position);
			this.SelectComponent(this.Document.AllComponents.First(c => c.Id == componentIds[componentIds.Length - 1]));
		}

		protected void SelectComponent(FineSoft.Models.ComponentObject component)
		{
			if (this.ContainerConfigContainer.DataContext == component)
				return;
			this.SelectedComponent = component;
			this.Cover.ShowMask(this.WebBrowser.GetRectangleOfComponent(this.SelectedComponent.Id));
		}

		//public FineSoft.Models.ComponentClass ViewModel_AddComponent
		//{
		//    set
		//    {
		//        this.AddComponent(value);
		//    }
		//}

		public static readonly System.Windows.DependencyProperty ViewModel_AddComponentProperty = System.Windows.DependencyProperty.Register("ViewModel_AddComponent", typeof(FineSoft.Models.ComponentClass), typeof(Page), new System.Windows.PropertyMetadata(ViewModel_AddComponent_Changed));
		public FineSoft.Models.ComponentClass ViewModel_AddComponent
		{
			get
			{
				return (FineSoft.Models.ComponentClass)this.GetValue(ViewModel_AddComponentProperty);
			}
			set
			{
				if (value == null)
					return;
				this.SetValue(ViewModel_AddComponentProperty, value);
				//this.SelectComponent(value);
			}
		}
		protected static void ViewModel_AddComponent_Changed(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
		{
			if (e.NewValue == null)
				return;
			((Page)d).AddComponent((FineSoft.Models.ComponentClass)e.NewValue);
		}

		public static readonly System.Windows.DependencyProperty ViewModel_SelectComponentProperty = System.Windows.DependencyProperty.Register("ViewModel_SelectComponent", typeof(System.Windows.Point), typeof(Page), new System.Windows.PropertyMetadata(ViewModel_SelectComponent_Changed));
		public System.Windows.Point ViewModel_SelectComponent
		{
			get
			{
				return (System.Windows.Point)this.GetValue(ViewModel_SelectComponentProperty);
			}
			set
			{
				if (value == null)
					return;
				this.SetValue(ViewModel_SelectComponentProperty, value);
				//this.SelectComponent(value);
			}
		}
		protected static void ViewModel_SelectComponent_Changed(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
		{
			((Page)d).SelectComponent((System.Windows.Point)e.NewValue);
		}

		//public System.Windows.Point ViewModel_SelectComponent
		//{
		//    set
		//    {
		//        this.SelectComponent(value);
		//    }
		//}
	}
}